//
//  SeleAddressView.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/9.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "SeleAddressView.h"
#import "SeleaddreTableViewCell.h"
#import "SeleAddressModel.h"

@interface SeleAddressView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) void(^clickBlock)(NSString *index,NSString *titst);
@property(nonatomic,strong)UITableView *lrftTableView;
@property(nonatomic,strong)UITableView *RightTableView;
@property(nonatomic,strong)UITableView *areaTableView;
@property(nonatomic,strong)NSMutableArray *leftDateSource;
@property(nonatomic,strong)NSMutableArray *rightDateSource;
@property(nonatomic,strong)NSMutableArray *areaDateSource;
@property(nonatomic,strong)UIView *tabBgview;

@end

@implementation SeleAddressView

- (instancetype)initWithFrame:(CGRect)frame click:(nonnull void (^)(NSString * _Nonnull, NSString * _Nonnull))select{
    
    if (self = [super initWithFrame:frame]) {
        //self = [[[NSBundle mainBundle] loadNibNamed:@"HFNewsDetailBottomView" owner:self options:nil] lastObject];
        //self.frame = frame;
        self.clickBlock = select;
        self.leftDateSource = [NSMutableArray arrayWithCapacity:1];
        self.rightDateSource = [NSMutableArray arrayWithCapacity:1];
        [self addSubview:self.tabBgview];
    }
    return self;
}

-(void)request{
    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [NetWorkTool POST:getManageAreast param:mudic success:^(id dic) {
        [self.leftDateSource removeAllObjects];
        KMyLog(@"dfdsdsfsdfds%@",dic);
        for (NSDictionary *mydic in [[dic objectForKey:@"data"] objectForKey:@"list1"]) {
            SeleAddressModel *model = [[SeleAddressModel alloc]init];
            [model setValuesForKeysWithDictionary:mydic];
            NSMutableArray *shiMu = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *shi in model.city) {
                SeleAddressModel *shiModel = [[SeleAddressModel alloc]init];
                [shiModel setValuesForKeysWithDictionary:shi];
                [shiMu addObject:shiModel];
                NSMutableArray *quMu = [NSMutableArray arrayWithCapacity:1];
                for (NSDictionary *qu in shiModel.area) {
                    SeleAddressModel *quModel = [[SeleAddressModel alloc]init];
                    [quModel setValuesForKeysWithDictionary:shi];
                    [quMu addObject:quModel];
                }
                shiModel.city =quMu;
            }
            
            model.city =shiMu;
            [self.leftDateSource addObject:model];
        }
        [self.lrftTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

-(void)requestwith:(SeleAddressModel *)model{
    if ([model.area_name isEqualToString:@"全部地区"]) {
        if (self.clickBlock) {
            self.clickBlock(@"",@"全部地区");
        }
        return;
    }
    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    mudic[@"area_parent_id"] = model.Id;
    
    [NetWorkTool POST:getManageAreast param:mudic success:^(id dic) {
        [self.rightDateSource removeAllObjects];
    
        KMyLog(@"dfdsdsfsdfds%@",dic);
        for (NSDictionary *mydic in [dic objectForKey:@"data"]) {
            SeleAddressModel *model = [[SeleAddressModel alloc]init];
            [model setValuesForKeysWithDictionary:mydic];
            model.isele = @"0";
            [self.rightDateSource addObject:model];
        }
        [self.RightTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _lrftTableView) {
        SeleAddressModel *model = [_leftDateSource safeObjectAtIndex:indexPath.row];
        
        for ( SeleAddressModel *model2  in _leftDateSource) {
            model2.isele = @"0";
            if ([model2.area_parent_id isEqualToString:model.area_parent_id]) {
                model2.isele = @"1";
            }
        }
        [self.lrftTableView reloadData];
        [self requestwith:model];
    }else{
        SeleAddressModel *model5 = [_rightDateSource safeObjectAtIndex:indexPath.row];
        if (self.clickBlock) {
            self.clickBlock(model5.area_parent_id,model5.area_name);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _lrftTableView) {
        return _leftDateSource.count;
    }else{
        return _rightDateSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SeleaddreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeleaddreTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView == _lrftTableView) {
        SeleAddressModel *model = [_leftDateSource safeObjectAtIndex:indexPath.row];
        cell.myLB.text = model.area_name;
        
        if ([model.isele isEqualToString:@"1"]) {
            cell.myLB.textColor = zhutiColor;
        }else{
            cell.myLB.textColor = [UIColor colorWithHexString:@"#5D5D5D"];
        }
    }else if (tableView == _RightTableView){
        SeleAddressModel *model = [_rightDateSource safeObjectAtIndex:indexPath.row];
        cell.myLB.text = model.area_name;
        if ([model.isele isEqualToString:@"1"]) {
            cell.myLB.textColor = zhutiColor;
        }else{
            cell.myLB.textColor = [UIColor colorWithHexString:@"#5D5D5D"];
        }
    }else{
        SeleAddressModel *model = [_rightDateSource safeObjectAtIndex:indexPath.row];
        cell.myLB.text = model.area_name;
    }
    return cell;
}

-(UIView *)tabBgview{
    if (!_tabBgview) {
        CGFloat www = (KWIDTH-50)/3;
        _tabBgview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 222)];
        _tabBgview.backgroundColor = [UIColor whiteColor];
        
        _lrftTableView = [[UITableView alloc]initWithFrame:CGRectMake(25, 2, www, 210)];
        _lrftTableView.delegate = self;
        _lrftTableView.dataSource = self;
        [_tabBgview addSubview:_lrftTableView];
        _lrftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_lrftTableView registerNib:[UINib nibWithNibName:@"SeleaddreTableViewCell" bundle:nil] forCellReuseIdentifier:@"SeleaddreTableViewCell"];
        [_lrftTableView reloadData];
        
        _RightTableView = [[UITableView alloc]initWithFrame:CGRectMake(_lrftTableView.right, 2, www, 210)];
        _RightTableView.delegate = self;
        _RightTableView.dataSource = self;
        [_tabBgview addSubview:_RightTableView];
        _RightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_RightTableView registerNib:[UINib nibWithNibName:@"SeleaddreTableViewCell" bundle:nil] forCellReuseIdentifier:@"SeleaddreTableViewCell"];
        [_RightTableView reloadData];
        
        _areaTableView = [[UITableView alloc]initWithFrame:CGRectMake(_lrftTableView.right, 2, www, 210)];
        _areaTableView.delegate = self;
        _areaTableView.dataSource = self;
        [_tabBgview addSubview:_areaTableView];
        _areaTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_areaTableView registerNib:[UINib nibWithNibName:@"SeleaddreTableViewCell" bundle:nil] forCellReuseIdentifier:@"SeleaddreTableViewCell"];
        [_areaTableView reloadData];
        [self request];
    }
    return _tabBgview;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
